package io.github.yylyingy.testweakreference;

import android.app.Activity;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created by Yangyl on 2017/8/5.
 */

public class Presenter {
    WeakReference<IView> mViewWeakReference;
    LinkedBlockingDeque<String> mBlockingDeque;

    public Presenter() {
        mBlockingDeque = new LinkedBlockingDeque<>();
        new Thread() {
            @Override
            public void run() {
                try {
                    System.gc();
                    mBlockingDeque.take();
                    Thread.sleep(1000);
                    Activity activity = (Activity) mViewWeakReference.get();
                    Log.d("Presenter", "is null?:" + (activity == null));
                    if (activity != null) {
                        Log.d("presenter","isdestroy?:" + activity.isDestroyed());
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void attactView(IView view) {
        mViewWeakReference = new WeakReference<>(view);
    }

    public void startGetView() {
        try {
            Thread.sleep(1);
            mBlockingDeque.add("");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
